<?php
namespace addons\prl\taglib;
use think\template\TagLib;
class Channel extends TagLib{
	/**
     * 定义标签列表
     */
    protected $tags   =  [
        'getchannelsnav'     => ['attr' => 'itemtpl,selectedids,disabledids,wraptag', 'close' => 0], //栏目一级导航 子节列表模板 选中ids 禁选ids 子列表包裹标签
        'getchannelsarr'     => ['attr' => 'item', 'close' => 1],//获取栏目数组
        'getchannelstreearr' => ['attr' => 'item', 'close' => 1],//获取树形数组
        'getchannelinfo'     =>	['attr' =>	'id,field,item,limit', 'close' => 1], //获取自定栏目 id 字段 变量
        'getchannelchildren' =>	['attr' => 'id,field,item', 'close' => 1],//获取指定栏目子栏目信息
        'getchannelbread'    => ['attr' => 'name,item'], //获取指定栏目的面包屑数据
    ];

    /**
     * 获取栏目数组 无任何结构
     * @param  string $item    循环变量
     */
    public function tagGetchannelsarr($tag, $content)
    {
        $item  = empty($tag['item']) ? 'vo' : $tag['item'];//循环变量名
        $parse = <<<parse
<?php
    \$__CHANNELS_CLASS__ = \addons\prl\service\Channels::init();
    \$__CHANNELS_ARR__ = \$__CHANNELS_CLASS__::getListArr();
?>
{volist name="__CHANNELS_ARR__" id="{$item}"}
{$content}
{/volist}
parse;
        return $parse;
    }

    /**
     * 获取栏目数组 无任何结构
     * @param  string $item    循环变量
     */
    public function tagGetchannelstreearr($tag, $content)
    {
        $item  = empty($tag['item']) ? 'vo' : $tag['item'];//循环变量名
        $parse = <<<parse
<?php
    \$__CHANNELS_CLASS__ = \addons\prl\service\Channels::init();
    \$__CHANNELS_ARR__ = \$__CHANNELS_CLASS__::getTreeListArr();
?>
{volist name="__CHANNELS_ARR__" id="{$item}"}
{$content}
{/volist}
parse;
        return $parse;
    }

    /**
     * 获取树形结构的html
     * @param  html  $itemtpl html结构
     * @param  string  $selectedids 选中id 多个用','隔开
     * @param  string  $disabledids 禁选id 多个用','隔开
     * @param  string  $wraptag     子栏目根dom
     * @param  string  $selectstring  选中字符串 默认selected
     * @return html
     */
    public function tagGetchannelsnav($tag)
    {
    	$itemtpl = empty($tag['itemtpl']) ? "<li value=@id @selected @disabled>@name @childlist</li>" : $tag['itemtpl'];
    	$selectedids = empty($tag['selectedids']) ? "" : $tag['selectedids'];
    	$disabledids = empty($tag['disabledids']) ? "" : $tag['disabledids'];
        $wraptag = empty($tag['wraptag']) ? 'ul' : $tag['wraptag'];
    	$selectstring = empty($tag['selectstring']) ? 'ul' : $tag['selectstring'];
        $selectedids = $this->autoBuildVar($selectedids);
    	$parse = <<<parse
<?php
    \$selectedids = {$selectedids};
    \$__CHANNELS_CLASS__ = \addons\prl\service\Channels::init();
    \$__CHANNELS_ITEMS__ = \$__CHANNELS_CLASS__::getTplTreeHtml('{$itemtpl}', \$selectedids, '{$disabledids}', '{$wraptag}');
    \$__CHANNELS_ITEMS__ = str_replace("selected", '{$selectstring}', \$__CHANNELS_ITEMS__);
    echo \$__CHANNELS_ITEMS__;
?>
parse;
        return $parse;
    }

    /**
     * 获取指定栏目信息
     * @param  int    $id    指定栏目id
     * @param  string $item  栏目变量
     * @param  int    $limit 数量 小于0时 不获取栏目的文章
     * @param  int    $page  页码数
     * @return channel_row => ['info' ,'data' => ['list','count']]
     */
    public function tagGetchannelinfo($tag, $content)
    {
    	$id = empty($tag['id']) ? '' : $tag['id'];
    	if(empty($id))
    		return false;
        $id = $this->autoBuildVar($id);
    	$item = empty($tag['item']) ? 'channel_row' : $tag['item'];
        $limit = empty($tag['limit']) ? 10 : $tag['limit'];
        $page = empty($tag['page']) ? 1 : $tag['page'];
    	$parse = <<<parse
<?php
    \$id = {$id};
    \$__CHANNELS_CLASS__ = \addons\prl\service\Channels::init();
    \${$item} = \$__CHANNELS_CLASS__::getInfo(\$id, '{$limit}', '{$page}');
?>
{$content}
parse;
		return $parse;
    }

    /**
     * 获取面包屑栏目
     * @param  int    $id      指定子栏目
     * @param  string item     变量
     * @return array           面包屑数据
     */
    public function tagGetchannelbread($tag, $content)
    {
        $id = empty($tag['id']) ? '' : $tag['id'];
        $item = empty($tag['item']) ? 'channel_bread' : $tag['item'];
        $name = $this->autoBuildVar($name);
        $parse = <<<parse
<?php
    \$id = {$id};
    \$__CHANNELS_CLASS__ = \addons\prl\service\Channels::init();
    \$__CHANNELS_BREAD__ = \$__CHANNELS_CLASS__::getBreadList(\$id);
?>
{volist name="__CHANNELS_BREAD__" id="{$item}"}
{$content}
{/volist}
parse;
    return $parse;
    }

    /**
     * 获取指定栏目子栏目数据 id,field,item
     * @param  int    $id      指定子栏目
     * @param  string field    字段
     * @param  string item     变量 
     * @return array           栏目数据
     */
    public function tagGetchannelchildren($tag, $content)
    {
        $id = empty($tag['id']) ? '' : $tag['id'];
        $item = empty($tag['item']) ? 'channel' : $tag['item'];
        $field = empty($tag['field']) ? 'id,name,href' : $tag['field'];
        $id = $this->autoBuildVar($id);
        $parse = <<<parse
<?php
    \$id = {$id};
    \$__CHANNELS_CLASS__ = \addons\prl\service\Channels::init();
    \$__CHANNELS_BREAD__ = \$__CHANNELS_CLASS__::getChannelChildrenList(\$id, '{$field}');
?>
{volist name="__CHANNELS_BREAD__" id="{$item}"}
{$content}
{/volist}
parse;
    return $parse;
    }
}